package com.zhugang.week03;

/**
 * @program algorithms
 * @description: linkedListQueue
 * @author: chanzhugang
 * @create: 2022/06/02 22:35
 */
public class LinkedListQueue {

    private Node head;
    private Node tail;

    /**
     * 入队
     *
     * @param value
     */
    public void enqueue(String value) {
        // 队尾插入数据
        Node newNode = new Node(value, null);
        if (tail == null) {
            // 队列为空
            head = newNode;
            tail = newNode;
        } else {
            // 尾部插入
            tail.next = newNode;
            tail = newNode;
        }
    }

    /**
     * 出队
     *
     * @return
     */
    public String dequeue() {
        // 队头删除数据
        if (head == null) {
            return null;
        }

        String data = head.data;
        head = head.next;
        if (head == null) {
            // 此时出队，队列已没有数据，更新tail = null
            tail = null;
        }
        return data;
    }

    public class Node {
        public String data;
        public Node next;

        public Node(String data, Node next) {
            this.data = data;
            this.next = next;
        }
    }
}